<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    barData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    barData: {
      deep: true,
      handler(val) {
        if (val && val.days && val.series) {
          this.initChart();
        }
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      if (this.barData && this.barData.days && this.barData.series) {
        this.initChart();
      }
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)

      const colors = ['#409EFF', '#36A3F7', '#F4516C'];
      
      const option = {
        backgroundColor: '#ffffff',
        title: {
          text: '奶茶周销量统计',
          textStyle: {
            fontSize: 16,
            fontWeight: 'normal',
            color: '#303133'
          },
          left: 'center',
          top: 0
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          backgroundColor: 'rgba(255, 255, 255, 0.9)',
          borderColor: '#f5f5f5',
          borderWidth: 1,
          textStyle: {
            color: '#666'
          },
          padding: [12, 16]
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          top: '20%',
          containLabel: true
        },
        legend: {
          data: this.barData.series ? this.barData.series.map(item => item.name) : [],
          top: 30,
          textStyle: {
            color: '#666',
            fontSize: 12
          },
          icon: 'rect',
          itemWidth: 10,
          itemHeight: 10,
          itemGap: 20
        },
        xAxis: [
          {
            type: 'category',
            data: this.barData.days || ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
            axisTick: {
              alignWithLabel: true,
              show: false
            },
            axisLine: {
              lineStyle: {
                color: '#e6e6e6'
              }
            },
            axisLabel: {
              color: '#666',
              fontSize: 12
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '杯数',
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#666',
              fontSize: 12
            },
            splitLine: {
              lineStyle: {
                color: '#ebeef5',
                type: 'dashed'
              }
            }
          }
        ],
        series: this.barData.series ? this.barData.series.map((item, index) => {
          return {
            name: item.name,
            type: 'bar',
            barWidth: '40%',
            itemStyle: {
              color: colors[index % colors.length]
            },
            data: item.data
          }
        }) : []
      };
      
      this.chart.setOption(option);
    }
  }
}
</script>